package com.phoenix.callAnalysis.db;

import java.util.ArrayList;

import com.phoenix.callAnalysis.models.MyCall;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.CallLog;
import android.util.Log;

public class MyCallLog {
	private static final String TAG = MyCallLog.class.getSimpleName();

	private final ContentResolver mContentResolver;

	public MyCallLog(Context context) {
		Log.d(TAG, "MyCallLog(Context c)");
		mContentResolver = context.getContentResolver();
	}

	public int getCallsCount() {
		Cursor calls = null;
		try {
			calls = mContentResolver.query(CallLog.Calls.CONTENT_URI, null,
					null, null, null);
			return calls.getCount();
		} catch (Exception e) {
			Log.e(TAG, "getCallsCount() ERROR!");
			return -1;
		} finally {
			if (calls != null && !calls.isClosed()) {
				calls.close();
			}
		}
	}

	public ArrayList<String> getAllNumbers() {
		ArrayList<String> numbers = new ArrayList<String>();
		Cursor calls = null;
		try {
			calls = mContentResolver.query(CallLog.Calls.CONTENT_URI, new String[]{CallLog.Calls.NUMBER},
					null, null, null);
			if(calls.getCount()==0) {
				return numbers;
			}
			calls.moveToFirst();
			while(!calls.isAfterLast()) {
				boolean duplicated = false;
				for(int i=0;i<numbers.size();i++) {
					if(calls.getString(0).equals(numbers.get(i))) {
						duplicated = true;
						break;
					}
				}
				if(!duplicated) {
					numbers.add(calls.getString(0));
				}
				calls.moveToNext();
			}
			return numbers;
		} catch (Exception e) {
			Log.e(TAG, "getAllNumbers() ERROR!");
			return numbers;
		} finally {
			if (calls != null && !calls.isClosed()) {
				calls.close();
			}
		}
	}

	public int getNumberCount() {
		return getAllNumbers().size();
	}

	public MyCall getLastestCall() {
		Cursor calls = null;
		MyCall call = new MyCall();
		try {
			calls = mContentResolver.query(CallLog.Calls.CONTENT_URI, null,
					null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
			if (calls.getCount() == 0)
				return call;
			calls.moveToFirst();
			call.setName(calls.getString(calls
					.getColumnIndex(CallLog.Calls.CACHED_NAME)));
			call.setNumberLabel(calls.getString(calls
					.getColumnIndex(CallLog.Calls.CACHED_NUMBER_LABEL)));
			call.setNumberType(calls.getInt(calls
					.getColumnIndex(CallLog.Calls.CACHED_NUMBER_TYPE)));
			call.setOccuredDate(calls.getLong(calls
					.getColumnIndex(CallLog.Calls.DATE)));
			call.setDuration(calls.getLong(calls
					.getColumnIndex(CallLog.Calls.DURATION)));
			call.setIsNew(calls.getInt(calls.getColumnIndex(CallLog.Calls.NEW)));
			call.setNumber(calls.getString(calls
					.getColumnIndex(CallLog.Calls.NUMBER)));
			call.setType(calls.getInt(calls.getColumnIndex(CallLog.Calls.TYPE)));
			return call;
		} catch (Exception e) {
			Log.e(TAG, "getLastestCall(Activity activity) ERROR!");
			return call;
		} finally {
			if (calls != null && !calls.isClosed()) {
				calls.close();
			}
		}
	}
}
